package srv

import (
	"arc_srv4/database"
	"arc_srv4/logutils"
	"arc_srv4/middleware"
	"fmt"
	"go.uber.org/zap"
)

func GenNewKey(name, phone, unit string, isUnit int) error {
	key, secret := middleware.GenKeySecret(phone)
	strSql := fmt.Sprintf("insert into t_api_user(appKey,appSecret,phone,name,unit,isunit,createdTime,status)"+
		"values(\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%d,now(),1)", key, secret, phone, name, unit, isUnit)

	res, err := database.Db.Engine.Exec(strSql)
	if err != nil {
		logutils.Logger.Error("insert into t_api_user error", zap.Error(err))
		return err
	}
	num, errs := res.RowsAffected()
	if errs != nil {
		logutils.Logger.Error("insert into t_api_user done but get rows affected error", zap.Error(err))
		return errs
	}

	logutils.Logger.Info("insert t_api_user success", zap.Int64("affected rows", num))
	return nil
}
